package sanshui.system.trade.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import sanshui.system.trade.common.CommonResponse;
import sanshui.system.trade.common.GlobalContain;
import sanshui.system.trade.common.TradeType;
import sanshui.system.trade.db.entity.TradeAccount;
import sanshui.system.trade.db.entity.TradeWorker;
import sanshui.system.trade.factory.data.TradeSystemType;
import sanshui.system.trade.factory.trade.worker.AbstractTradeWorker;
import sanshui.system.trade.model.PositionAccModel;
import sanshui.system.trade.model.TradeAccountModel;
import sanshui.system.trade.model.TradeReportModel;
import sanshui.system.trade.model.TradeWorkerModel;
import sanshui.system.trade.pojo.ao.CreateAccountAo;
import sanshui.system.trade.pojo.ao.CreateBackTradeAo;
import sanshui.system.trade.pojo.ao.QueryAccAo;
import sanshui.system.trade.pojo.vo.TradeAccountVo;
import sanshui.system.trade.service.IAccountService;
import sanshui.system.trade.service.ITradeHisOpPosService;
import sanshui.system.trade.service.IWorkerService;
import sanshui.system.trade.util.CommonTimeUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@ApiOperation("账户接口")
@Slf4j
@RestController
@RequestMapping("/account")
public class AccController {
    @Resource
    IAccountService accountService;
    @Resource
    IWorkerService workerService;
    @Resource
    ITradeHisOpPosService posService;

    @ApiOperation(value = "创建实时交易账户")
    @RequestMapping(value = "/createAccount", method = RequestMethod.POST)
    public CommonResponse<TradeAccountModel> createAccount(@RequestBody CreateBackTradeAo backTradeAo) {
        TradeWorkerModel tradeWorkerModel = new TradeWorkerModel();
        BeanUtil.copyProperties(backTradeAo, tradeWorkerModel);
        tradeWorkerModel.setEndTime("");
        tradeWorkerModel.setTradeSystemType(TradeSystemType.RT.name());
        log.info("创建实时交易tradeWorkerModel:{}", tradeWorkerModel);

        return CommonResponse.ok(accountService.createAccount(tradeWorkerModel), "创建成功");
    }

    @ApiOperation(value = "查找账户")
    @RequestMapping(value = "/findAccount", method = RequestMethod.POST)
    public CommonResponse<List<TradeWorkerModel>> findAccount(@RequestBody QueryAccAo queryAccAo) {
        List<TradeAccount> account = accountService.findAccount(queryAccAo);
        List<TradeWorkerModel> res = new ArrayList<>();
        for (TradeAccount tradeAccount : account) {
            List<TradeWorker> tradeWorkers = workerService.selectWorkerByAccId(tradeAccount.getAccId());
            for (TradeWorker tradeWorker : tradeWorkers) {
                TradeWorkerModel tradeWorkerModel = new TradeWorkerModel();
                tradeWorkerModel.setAccId(tradeAccount.getAccId());
                tradeWorkerModel.setWorkerId(tradeWorker.getWorkerId());
                tradeWorkerModel.setWorkerStatus(tradeWorker.getWorkerStatus());
                tradeWorkerModel.setInitValue(String.valueOf(tradeAccount.getInitValue() / 100.0));
                tradeWorkerModel.setCurrentCash(String.valueOf(tradeAccount.getCurrentCash() / 100.0));
                tradeWorkerModel.setCurrentValue(String.valueOf(tradeAccount.getCurrentValue() / 100.0));
                tradeWorkerModel.setTradeMarketKey(tradeAccount.getAccountType());
                tradeWorkerModel.setAccDesc(tradeAccount.getAccDesc());
                tradeWorkerModel.setTradeSystemType(tradeWorker.getTradeSystemType());
                res.add(tradeWorkerModel);
            }
        }
        return CommonResponse.ok(res, "查找账户成功");
    }

    @ApiOperation(value = "查找账户下的持仓")
    @RequestMapping(value = "/findAccPos", method = RequestMethod.POST)
    public CommonResponse<Map<String, PositionAccModel>> findAccPos(@RequestBody QueryAccAo queryAccAo) {
        return CommonResponse.ok(posService.selectAccHoldPosModels(Long.parseLong(queryAccAo.getAccId()), CommonTimeUtil.getNowTime16()), "创建成功");
    }

//    @ApiOperation(value = "停止运行")
//    @RequestMapping(value = "/stopRun", method = RequestMethod.POST)
//    public CommonResponse<Boolean> stopRun(@RequestBody QueryAccAo queryAccAo) {
//        return CommonResponse.ok(posService.selectAccHoldPosModels(Long.parseLong(queryAccAo.getAccId()), CommonTimeUtil.getNowTime16()),"创建成功");
//    }

    @ApiOperation(value = "删除账户")
    @RequestMapping(value = "/deleteAccount/{workerId}", method = RequestMethod.GET)
    public CommonResponse<String> deleteAccount(@PathVariable("workerId") String workerId) {
        accountService.deleteAccWorker(Long.parseLong(workerId));
        return CommonResponse.ok( "删除成功");
    }
}